package spring.aop;

import spring.AopConfigException;
import spring.beans.factory.BeanFactory;

import java.lang.reflect.Proxy;
import java.util.List;

/**
 * AOP代理工厂
 */
public class  AopProxyFactory {

    public AopProxy createAopProxy(Object bean, String beanName, List<Advisor> matchAdvisors, BeanFactory beanFactory)
            throws Exception {

        Class<?> targetClass = bean.getClass();

        if (targetClass == null) {
            throw new AopConfigException("TargetSource cannot determine target class: " +
                    "Either an interface or a target is required for proxy creation.");
        }

        // 如果目标类是一个接口或者是 java.lang.reflect.Proxy 的子类 则使用 JDK 动态代理
        // 源码中用户可以通过设置参数强制使用Cglib

        if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
//            return new JdkDynamicAopProxy(beanName, bean, matchAdvisors, beanFactory);
        } else {
            return new CglibDynamicAopProxy(beanName, bean, matchAdvisors, beanFactory);
        }
        return (AopProxy) bean;
    }




}


